import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

class HbSwiperPagination2 extends SwiperPlugin {
  @override
  Widget build(BuildContext context, SwiperPluginConfig config) {
    return ConfigProvider(config: config, child: HbSwiperPaginationWidget2());
  }
}

class HbSwiperPaginationWidget2 extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => HbSwiperPaginationWidgetState2();
}

class HbSwiperPaginationWidgetState2 extends State<HbSwiperPaginationWidget2> {
  SwiperPluginConfig config;

  double page;

  int itemCount;

  double pageinationItemWidth = 20;

  double pageinationHeight = 2;

  @override
  void didChangeDependencies() {
    config = ConfigProvider.of(context).config;

    page = config.pageController.page;
    itemCount = config.itemCount;
    config.pageController.addListener(() {
      setState(() {
        page = config.pageController.page;
      });
    });
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {

    return Align(
      alignment: Alignment.bottomCenter,
      child: Padding(
        padding: EdgeInsets.only(right: 18, bottom: 18),
        child: Stack(
          alignment: Alignment.center,
          children: [
            Container(
              width: pageinationItemWidth * itemCount,
              height: pageinationHeight,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(pageinationHeight/2),
                color: Colors.grey,
              ),
            ),
            Positioned(
              left: page * pageinationItemWidth,
              child: Container(
                width: pageinationItemWidth,
                height: pageinationHeight,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(pageinationHeight/2),
                  color: Colors.white,
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    config.pageController.dispose();
    super.dispose();
  }
}

class ConfigProvider extends InheritedWidget {
  final SwiperPluginConfig config;
  final Widget child;

  ConfigProvider({this.config, this.child}) : super(child: child);

  static ConfigProvider of(BuildContext context) =>
      context.dependOnInheritedWidgetOfExactType<ConfigProvider>();

  @override
  bool updateShouldNotify(ConfigProvider oldWidget) {
    return oldWidget.config != config;
  }
}
